import json

from django.http import JsonResponse
from django.views import View
from lapi_app.models.environment_model.environment import Environment
from lapi_app.view.user_view.token_check import access_token


class Env_list(View):

    def post(self, request, *args, **kwargs):
        data = json.loads(request.body)
        if not access_token(data["user_id"]):
            return JsonResponse({
                "code": 1001,
                "message": "登录失效，请重新登录"
            })
        envs = Environment.objects.all().order_by("-id")
        env_list = []
        for env in envs:
            env_dict = {
                "environment_id": env.id,
                "environment_name": env.e_name + "(" + env.e_address + ")"
            }
            env_list.append(env_dict)
        return JsonResponse({
            "code": 200,
            "message": "获取环境列表成功",
            "content": env_list
        })
